/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.enums.BooleanTypeEnum;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserAddress;
import com.pig4cloud.pigx.shop.user.service.ShopUserAddressService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址
 *
 * @author pigx code generator
 * @date 2023-12-03 22:42:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/address")
@Tag(description = "shop_user_address", name = "用户地址管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ShopUserAddressController {

	private final ShopUserAddressService shopUserAddressService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param shopUserAddress 用户地址
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getShopUserAddressPage(Page page, ShopUserAddress shopUserAddress) {
		return R.ok(shopUserAddressService.page(page, Wrappers.query(shopUserAddress)));
	}

	/**
	 * 通过id查询用户地址
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(shopUserAddressService.getById(id));
	}

	/**
	 * 新增用户地址
	 * @param shopUserAddress 用户地址
	 * @return R
	 */
	@Operation(summary = "新增用户地址", description = "新增用户地址")
	@SysLog("新增用户地址")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('shop_user_address_add')")
	public R save(@RequestBody ShopUserAddress shopUserAddress) {
		return R.ok(shopUserAddressService.save(shopUserAddress));
	}

	/**
	 * 修改用户地址
	 * @param shopUserAddress 用户地址
	 * @return R
	 */
	@Operation(summary = "修改用户地址", description = "修改用户地址")
	@SysLog("修改用户地址")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('shop_user_address_edit')")
	public R updateById(@RequestBody ShopUserAddress shopUserAddress) {
		return R.ok(shopUserAddressService.updateById(shopUserAddress));
	}

	/**
	 * 通过id删除用户地址
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除用户地址", description = "通过id删除用户地址")
	@SysLog("通过id删除用户地址")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('shop_user_address_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(shopUserAddressService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param shopUserAddress 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('shop_user_address_export')")
	public List<ShopUserAddress> export(ShopUserAddress shopUserAddress) {
		return shopUserAddressService.list(Wrappers.query(shopUserAddress));
	}

	/**
	 * c端添加用户地址
	 * @param shopUserAddress
	 * @return
	 */
	@PostMapping("/c/add")
	public R add(@RequestBody ShopUserAddress shopUserAddress) {
		Long userId = SecurityUtils.getUser().getId();
		shopUserAddress.setShopUserId(userId);
		return R.ok(shopUserAddressService.add(shopUserAddress));
	}

	/**
	 * c端更新用户地址
	 * @param shopUserAddress
	 * @return
	 */
	@PostMapping("/c/update")
	public R update(@RequestBody ShopUserAddress shopUserAddress) {
		Long userId = SecurityUtils.getUser().getId();
		return R.ok(shopUserAddressService.update(shopUserAddress));
	}

	/**
	 * c端删除用户地址
	 * @param shopUserAddress
	 * @return
	 */
	@PostMapping("/c/delete")
	public R delete(@RequestBody ShopUserAddress shopUserAddress) {
		return R.ok(shopUserAddressService.removeById(shopUserAddress.getId()));
	}

	/**
	 * 分页获取用户地址
	 * @param page
	 * @param shopUserAddress
	 * @return
	 */
	@GetMapping("/c/page")
	public R page(Page page, ShopUserAddress shopUserAddress) {
		Long userId = SecurityUtils.getUser().getId();
		LambdaQueryWrapper<ShopUserAddress> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShopUserAddress::getShopUserId, userId);
		lqw.orderByDesc(ShopUserAddress::getIsDefault);
		lqw.orderByDesc(ShopUserAddress::getCreateTime);
		lqw.eq(ShopUserAddress::getDelFlag, DeleteFlagEnum.VALID.getType());
		return R.ok(shopUserAddressService.page(page, lqw));
	}

	/**
	 * 获取c端地址详情
	 * @param shopUserAddress
	 * @return
	 */
	@GetMapping("/c/info")
	public R getAddress(ShopUserAddress shopUserAddress) {
		return R.ok(shopUserAddressService.getById(shopUserAddress.getId()));
	}

	/**
	 * 获取c端用户的默认地址
	 * @return
	 */
	@GetMapping("/c/default")
	public R defaultAddress() {
		Long userId = SecurityUtils.getUser().getId();
		LambdaQueryWrapper<ShopUserAddress> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ShopUserAddress::getShopUserId, userId);
		queryWrapper.eq(ShopUserAddress::getIsDefault, BooleanTypeEnum.TRUE.getType());
		queryWrapper.eq(ShopUserAddress::getDelFlag, DeleteFlagEnum.VALID.getType());
		return R.ok(shopUserAddressService.getOne(queryWrapper, false));
	}

}
